<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="考勤员工ID，外键关联员工表" prop="userid">
        <el-input
          v-model="queryParams.userid"
          placeholder="请输入考勤员工ID，外键关联员工表"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="打卡日期" prop="clockintime">
        <el-date-picker clearable
          v-model="queryParams.clockintime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择打卡日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="打卡时间" prop="clockouttime">
        <el-date-picker clearable
          v-model="queryParams.clockouttime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择打卡时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:attendance:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:attendance:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:attendance:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:attendance:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="attendanceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="考勤id" align="center" prop="attendanceid" />
      <el-table-column label="考勤员工ID" align="center" prop="userid" />
      <el-table-column label="上班时间" align="center" prop="clockintime" />

      <el-table-column label="下班时间" align="center" prop="clockouttime" />

      <el-table-column label="打卡类型，IN为上班打卡，OUT为下班打卡,ABSENCE为缺勤,LATE为迟到,EARLY为早退" align="center" prop="type" />
      
      <el-table-column prop="type" label="打卡状态" width="80" :filters="[
              { text: '迟到', value: '迟到' },
              { text: '早退', value: '早退' },
              { text: '正常打卡', value: '正常打卡' },
            ]" :filter-method="filtertype" filter-placement="bottom-end">
              <template #default="scope">
                <el-tag :type="scope.row.type === '早退' ? '' : scope.row.type === '迟到' ? 'warning' : 'success' " disable-transitions>{{
                  scope.row.type }}</el-tag>
              </template>
            </el-table-column>
      
      
      
      
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:attendance:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:attendance:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <el-pagination      
  :total="total"
  v-model:currentPage="queryParams.pageNum"
  v-model:pageSize="queryParams.pageSize"
  @size-change="handleSizeChange"
  @current-change="handleCurrentChange"
  layout="total, sizes, prev, pager, next, jumper"
/>

    <!-- 添加或修改存储员工考勤记录对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="attendanceRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="考勤员工ID，外键关联员工表" prop="EmployeeID">
          <el-input v-model="form.EmployeeID" placeholder="请输入考勤员工ID，外键关联员工表" />
        </el-form-item>
        <el-form-item label="打卡日期" prop="PunchDate">
          <el-date-picker clearable
            v-model="form.PunchDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择打卡日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="打卡时间" prop="PunchTime">
          <el-date-picker clearable
            v-model="form.PunchTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择打卡时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Attendance">
import { listAttendance, getAttendance, delAttendance, addAttendance, updateAttendance } from "@/api/system/attendance";
import { getCurrentInstance,ref,reactive,toRefs} from 'vue';
const { proxy } = getCurrentInstance();
const queryRef = ref(null); // 定义 ref 引用

const attendanceList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    EmployeeID: null,
    PunchDate: null,
    PunchTime: null,
    Type: null
  },
  rules: {
  }
});

// 
function handleSizeChange(newSize) {
  queryParams.pageSize = newSize;
  queryParams.pageNum = 1; // 重置页码为第一页
  getList();
}

function handleCurrentChange(newPage) {
  queryParams.pageNum = newPage;
  getList();
}

const { queryParams, form, rules } = toRefs(data);

/** 查询存储员工考勤记录列表 */
function getList() {
  loading.value = true;
  listAttendance(queryParams.value).then(response => {
    attendanceList.value = response.list;
    total.value = response.total;
    loading.value = false;
  });
}

//筛选迟到状态 
// 筛选打卡状态
function filtertype(value, row) {
  return row.type === value;
}
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    AttendanceID: null,
    EmployeeID: null,
    PunchDate: null,
    PunchTime: null,
    Type: null
  };
  proxy.resetForm("attendanceRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  if (queryRef.value) {
    queryRef.value.resetFields();
  }
  queryParams.pageNum = 1;
  queryParams.pageSize = 10;
  queryParams.name = null;
  queryParams.username = null;
  queryParams.phone = null;
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.AttendanceID);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加存储员工考勤记录";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _AttendanceID = row.AttendanceID || ids.value
  getAttendance(_AttendanceID).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改存储员工考勤记录";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["attendanceRef"].validate(valid => {
    if (valid) {
      if (form.value.AttendanceID != null) {
        updateAttendance(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addAttendance(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _AttendanceIDs = row.AttendanceID || ids.value;
  proxy.$modal.confirm('是否确认删除存储员工考勤记录编号为"' + _AttendanceIDs + '"的数据项？').then(function() {
    return delAttendance(_AttendanceIDs);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/attendance/export', {
    ...queryParams.value
  }, `attendance_${new Date().getTime()}.xlsx`)
}

getList();
</script>
